package com.seedeer.project.controller.system;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seedeer.framework.aspectj.lang.annotation.Log;
import com.seedeer.framework.web.domain.AjaxResult;
import com.seedeer.project.domain.entity.SysParam;
import com.seedeer.project.service.system.ISysParamService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 * 系统参数 前端控制器
 * </p>
 *
 * @author wangqiang
 */
@Api(tags = {"SysParam"}, description = "系统参数相关接口")
@RestController
@RequestMapping(value = "/api/sysParam")
public class SysParamController {

    @Autowired
    private ISysParamService sysParamService;

    @ApiOperation(value = "主参数分页列表")
    @RequestMapping(value = {"/mainPage"}, method = RequestMethod.GET)
    public IPage<SysParam> mainPage(@RequestParam(value = "page", defaultValue = "1") Integer current,
                                    @RequestParam(value = "size", defaultValue = "10") Integer size,
                                    SysParam sysParam) {
        Wrapper<SysParam> wrapper = Wrappers.<SysParam>lambdaQuery()
                .like(StringUtils.isNotBlank(sysParam.getParamCode()), SysParam::getParamCode, sysParam.getParamCode())
                .like(StringUtils.isNotBlank(sysParam.getParamNameCn()), SysParam::getParamNameCn, sysParam.getParamNameCn())
                .like(StringUtils.isNotBlank(sysParam.getParamNameEn()), SysParam::getParamNameEn, sysParam.getParamNameEn())
                .isNull(SysParam::getParentCode);
        return sysParamService.page(new Page<>(current, size), wrapper);
    }

    @ApiOperation(value = "子参数分页列表")
    @RequestMapping(value = {"/subPage"}, method = RequestMethod.GET)
    public IPage<SysParam> subPage(@RequestParam(value = "page", defaultValue = "1") Integer current,
                                   @RequestParam(value = "size", defaultValue = "10") Integer size,
                                   @RequestParam(value = "parentCode") String parentCode) {
        Wrapper<SysParam> wrapper = Wrappers.<SysParam>lambdaQuery()
                .eq(SysParam::getParentCode, parentCode)
                .orderByAsc(SysParam::getParamSort)
                .orderByAsc(SysParam::getId);
        return sysParamService.page(new Page<>(current, size), wrapper);
    }

    @ApiOperation(value = "根据主参查询子参")
    @RequestMapping(value = {"/findSubParam"}, method = RequestMethod.GET)
    public AjaxResult findSubParam(@RequestParam(value = "parentCode") String parentCode) {
        return AjaxResult.success(sysParamService.findSubParam(parentCode));
    }

    @ApiOperation(value = "查询参数")
    @RequestMapping(value = {"/findParam"}, method = RequestMethod.GET)
    public AjaxResult findParam(@RequestParam(value = "paramCode") String paramCode) {
        return AjaxResult.success(sysParamService.findParam(paramCode));
    }

    @Log
    @ApiOperation(value = "新增参数")
    @RequestMapping(value = {"/addParam"}, method = RequestMethod.POST)
    public AjaxResult addParam(@RequestBody SysParam sysParam) {
        sysParamService.save(sysParam);
        return AjaxResult.success();
    }

    @Log
    @ApiOperation(value = "更新参数")
    @RequestMapping(value = {"/updateParam"}, method = RequestMethod.PUT)
    public AjaxResult updateParam(@RequestBody SysParam sysParam) {
        Wrapper<SysParam> wrapper = Wrappers.<SysParam>lambdaQuery()
                .eq(SysParam::getId, sysParam.getId());
        sysParamService.update(sysParam, wrapper);
        return AjaxResult.success();
    }

    @Log
    @ApiOperation(value = "删除参数")
    @RequestMapping(value = {"/{id}"}, method = RequestMethod.DELETE)
    public AjaxResult delete(@PathVariable Integer id) {
        sysParamService.removeById(id);
        return AjaxResult.success();
    }

}
